package com.starsea.book.ui.newpage.utils

import android.text.TextUtils
import com.blankj.utilcode.constant.TimeConstants
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale


/**
 * @author Lee
 * @createTime 2023-08-16 15 GMT+8
 * @desc :
 */
object Singleton {

    fun getMessageListTime(millis: Long): String? {
        val now = System.currentTimeMillis()
        val span = now - millis
        if (span < 0)
            return "刚刚"
        if (span < 1000) {
            return "刚刚"
        } else if (span < TimeConstants.MIN) {
            return String.format(Locale.getDefault(), "%d秒", span / TimeConstants.SEC)
        } else if (span < TimeConstants.HOUR) {
            return String.format(Locale.getDefault(), "%d分钟", span / TimeConstants.MIN)
        }else if (span < TimeConstants.DAY) {
            return String.format(Locale.getDefault(), "%d天", span / TimeConstants.HOUR)
        }
        val wee = getWeeOfToday()
        return if (millis >= wee) {
            String.format("今天%tR", millis)
        } else if (millis >= wee - TimeConstants.DAY) {
            String.format("昨天%tR", millis)
        } else if (millis >= wee - TimeConstants.DAY * 2) {
            String.format("前天%tR", millis)
        } else {
            var isThisYear = false
            if (timeStamp2Date(System.currentTimeMillis(), "yyyy")
                    .equals(timeStamp2Date(millis, "yyyy"))
            ) {
                isThisYear = true
            }
            return if (isThisYear) {
                fixTimeText(timeStamp2Date(millis, "MM-dd HH:mm"))
            } else {
                timeStamp2Date(millis, "yyyy年MM月dd日 HH:mm")
            }

        }
    }


    fun getFullTime(millis: Long): String {
        return timeStamp2Date(millis, "yyyy-MM-dd HH:mm:ss")
    }

    fun getLableTime(millis: Long): String {
        return timeStamp2Date(millis, "yyyy年MM月dd日")
    }


    private fun getWeeOfToday(): Long {
        val cal = Calendar.getInstance()
        cal[Calendar.HOUR_OF_DAY] = 0
        cal[Calendar.SECOND] = 0
        cal[Calendar.MINUTE] = 0
        cal[Calendar.MILLISECOND] = 0
        return cal.timeInMillis
    }

    fun timeStamp2Date(timeStamp: Long, format: String): String {
        val sdf = SimpleDateFormat(format, Locale.getDefault())
        return sdf.format(Date(timeStamp))
    }

    fun fixTimeText(temp: String): String {
        return try {
            var tempText = temp.replace(" 0", " ")//替换 04-04 09_45     ==》  04-04 9_45

            if (tempText[0].toString() == "0") {
                tempText = tempText.substring(1, tempText.length)// 04-04 9_45    ==>  4_04 9_45
            }
            tempText
        } catch (e: Exception) {
            temp
        }
    }


    fun formatSecondsTo00(timeSeconds: Int): String {
        val second = timeSeconds % 60
        val minuteTemp = timeSeconds / 60
        return if (minuteTemp > 0) {
            val minute = minuteTemp % 60
            val hour = minuteTemp / 60
            if (hour > 0) {
                ((if (hour >= 10) hour.toString() + "" else "0$hour") + ":" + (if (minute >= 10) minute.toString() + "" else "0$minute") + ":"
                        + if (second >= 10) second.toString() + "" else "0$second")
            } else {
                (if (minute >= 10) minute.toString() + "" else "0$minute") + ":" + if (second >= 10) second.toString() + "" else "0$second"
            }
        } else {
            "00:" + if (second >= 10) second.toString() + "" else "0$second"
        }
    }

    fun getTimeMins(time: String?): Long {
        var mTime = time
        if (TextUtils.isEmpty(time)) {
            mTime = timeStampToDate(System.currentTimeMillis())
        }
        val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        var date: Date? = null
        try {
            date = format.parse(mTime!!)
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return date?.time ?: System.currentTimeMillis()
    }

    fun timeStampToDate(timeStamp: Long): String? {
        val date = Date(timeStamp)
        val simpleDateFormat =
            SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        return simpleDateFormat.format(date)
    }

    fun getTimeMin(time: String?): Long {
        val format = SimpleDateFormat("yyyy-MM-dd")
        var date: Date? = null
        try {
            date = format.parse(time)
        } catch (e: ParseException) {
            e.printStackTrace()
        }

        return date!!.time
    }

    fun timeStampYMD(timeStamp: Long): String {
        val date = Date(timeStamp)
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd")
        return simpleDateFormat.format(date)
    }

    fun timeStampY(timeStamp: Long): String {
        val date = Date(timeStamp)
        val simpleDateFormat = SimpleDateFormat("yyyy")
        return simpleDateFormat.format(date)
    }

    fun getWeekByDateInt(dateStr: String): Int {
        val year = dateStr.substring(0, 4).toInt()
        val month = dateStr.substring(5, 7).toInt()
        val day = dateStr.substring(8, 10).toInt()
        val c = Calendar.getInstance()
        c[Calendar.YEAR] = year
        c[Calendar.MONTH] = month - 1
        c[Calendar.DAY_OF_MONTH] = day
        var week = 0
        val weekIndex = c[Calendar.DAY_OF_WEEK]
        when (weekIndex) {
            1 -> week = 7
            2 -> week = 1
            3 -> week = 2
            4 -> week = 3
            5 -> week = 4
            6 -> week = 5
            7 -> week = 6
        }
        return week
    }

    fun timeStampMDHS(timeStamp: Long): String {
        val date = Date(timeStamp)
        val simpleDateFormat = SimpleDateFormat("MM-dd HH:mm")
        return simpleDateFormat.format(date)
    }

    fun timeStampYMDHS(timeStamp: Long): String {
        val date = Date(timeStamp)
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm")
        return simpleDateFormat.format(date)
    }

    fun timeStampHS(timeStamp: Long): String {
        val date = Date(timeStamp)
        val simpleDateFormat = SimpleDateFormat("HH:mm")
        return simpleDateFormat.format(date)
    }

    fun timeStampMD(timeStamp: Long): String {
        val date = Date(timeStamp)
        val simpleDateFormat = SimpleDateFormat("MM月dd日")
        return simpleDateFormat.format(date)
    }

    fun timeStampYMText(timeStamp: Long): String {
        val date = Date(timeStamp)
        val simpleDateFormat = SimpleDateFormat("yyyy年MM月")
        return simpleDateFormat.format(date)
    }

    fun getWeekByDateStr(dateStr: String): String {
        val year = dateStr.substring(0, 4).toInt()
        val month = dateStr.substring(5, 7).toInt()
        val day = dateStr.substring(8, 10).toInt()
        val c = Calendar.getInstance()
        c[Calendar.YEAR] = year
        c[Calendar.MONTH] = month - 1
        c[Calendar.DAY_OF_MONTH] = day
        var week = ""
        val weekIndex = c[Calendar.DAY_OF_WEEK]
        when (weekIndex) {
            1 -> week = "周日"
            2 -> week = "周一"
            3 -> week = "周二"
            4 -> week = "周三"
            5 -> week = "周四"
            6 -> week = "周五"
            7 -> week = "周六"
        }
        return week
    }


}